package cc.wanforme.chipmunity.system.service;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import cc.wanforme.chipmunity.common.bean.WebResponse;
import cc.wanforme.chipmunity.common.consts.PassageEnum;
import cc.wanforme.chipmunity.system.event.BaseEvent;
import cc.wanforme.chipmunity.system.event.ReplyEvent;
import cc.wanforme.chipmunity.system.mapper.PassageMapper;
import cc.wanforme.chipmunity.system.mapper.ReplyMapper;
import cc.wanforme.chipmunity.system.mapper.user.UserMapper;
import cc.wanforme.chipmunity.system.po.Passage;
import cc.wanforme.chipmunity.system.po.Reply;
import cc.wanforme.chipmunity.system.po.user.User;
import cc.wanforme.chipmunity.system.service.interfaces.ReplyService;

/**
 * @author wanne
 * 2019年8月17日
 * 
 */
@Service
public class ReplyServiceImpl implements ReplyService, ApplicationEventPublisherAware{

	private ApplicationEventPublisher publisher;
	
	@Autowired
	private ReplyMapper replyMapper;
	
	@Autowired
	private PassageMapper passageMapper;

	@Autowired
	private UserMapper userMapper;
	
	@Override
	public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
		this.publisher = applicationEventPublisher;
	}
	
	@Override
	public WebResponse saveReply(Reply reply) {
		if(reply.getPassageId()==null ) {
			return WebResponse.fail("未知文章");
		}
		if(StringUtils.isBlank(reply.getRtfContent())) {
			return WebResponse.fail("请填写内容");
		}

		Passage passage = passageMapper.selectPassage(reply.getId());
		if(passage == null) {
			return WebResponse.fail("未知文章");
		}
		
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		if(authentication == null) {
			return WebResponse.fail("未登录");
		}
		
		//检查文章状态
		if( !passage.getState().equals( PassageEnum.STATE_ON.getState() )) {
			return WebResponse.fail("文章不见了");
		}
		
		User user = userMapper.selectUserByUsername(authentication.getName());
		
		Reply reply_persistent = new Reply(passage.getId(), user.getId(), passage.getReplyNum(), reply.getRtfContent());
		replyMapper.insertReply(reply_persistent);
		return WebResponse.success("回复成功");
	}
	
	
	@Override
	public WebResponse updateReply(Reply reply) {
		if(reply.getId() == null) {
			return WebResponse.fail("未知回复");
		}
		
		replyMapper.updateReply(reply);
		if(reply.getState()!=null) {
			publisher.publishEvent(new ReplyEvent(reply, BaseEvent.OperateType.UPDATE_DELETE));
		}
		
		return WebResponse.fail("删除成功");
	}

	@Override
	public WebResponse deleteReply(Long id) {
		if(id == null) {
			return WebResponse.fail("未知回复");
		}
		
		Reply reply = replyMapper.selectReply(id);
		publisher.publishEvent(new ReplyEvent(reply, BaseEvent.OperateType.DELETE));
		replyMapper.deleteReply(id);
		return WebResponse.success("删除成功");
	}
}
